/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.service.processor.active;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.account.core.domain.account.Account;
import info.peigen.hotpot.business.account.core.domain.account.AccountTransfer;
import info.peigen.hotpot.business.account.core.service.processor.base.AbstractAccountServiceProcessor;
import info.peigen.hotpot.business.account.facade.order.trans.TransferOrder;
import info.peigen.hotpot.common.core.lang.money.Money;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

import static info.peigen.hotpot.business.account.facade.enums.AccountFeeAction.Free;


/**
 * <b>(ActiveFeeAccountProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/26
 */
@Component
public class ActiveFeeAccountProcessor extends AbstractAccountServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 12;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        TransferOrder order = (TransferOrder) serviceContext.getOrder();

        // 收费
        if (ObjectUtil.isNotNull(order.getFeeOrderInfo()) && order.getFeeOrderInfo().getAction() != Free) {
            Account feePayeeAccount = accountRepository.lock(order.getFeeOrderInfo().getPayeeAccountId());
            Assert.notNull(feePayeeAccount, () -> new BusinessException(ResultCode.FAILURE, "未能锁定账户[accountId=" + order.getFeeOrderInfo().getPayeeAccountId() + "]"));

            AccountTransfer transfer        = (AccountTransfer) serviceContext.getDomain();
            Account         feePayerAccount = getFeeAccount(order.getFeeOrderInfo().getPayerAccountId(), transfer.getPayerAccount(), transfer.getPayeeAccount());
            transfer.setFeePayerAccount(feePayerAccount).setFeePayeeAccount(feePayeeAccount).setHistoryFeePayerBalance(new Money(feePayerAccount.getBalance())).setHistoryFeePayeeBalance(new Money(feePayeeAccount.getBalance())).setFeeAmount(order.getFeeOrderInfo().getFeeAmount()).setFeeMemo(order.getFeeOrderInfo().getMemo());
            serviceContext.setDomain(transfer);
        }

    }

    private Account getFeeAccount(String feeAccountId, Account payerAccount, Account payeeAccount) {
        if (ObjectUtil.isNotNull(payerAccount) && StrUtil.equals(feeAccountId, payerAccount.getAccountId())) {
            return payerAccount;
        } else if (ObjectUtil.isNotNull(payeeAccount) && StrUtil.equals(feeAccountId, payeeAccount.getAccountId())) {
            return payeeAccount;
        } else {
            return accountRepository.active(feeAccountId);
        }
    }
}
